
export function menu_parents(p_menus) {
  let menus = JSON.parse(JSON.stringify(p_menus))
  if (!menus instanceof Array) {
    return false
  }

  let ret = []
  menus.forEach((item, index) => {
    if (item.level == 1) {
      let c = menu_first_child(menus, item.id)
      if (c) {
        if(c.path) {
          item.path = c.path
        }else{
          let cc = menu_first_child(menus, c.id)
          item.path = cc.path
        }
      }
      if (item.isHidden !== true) {
        ret.push(item)
      }
    }
  })

  return ret
}

export function menu_first_child(menus, parentId) {
  let index = menus.findIndex((item, index) => {
    if (item.isHidden) return false
    return item.parentId == parentId
  })

  if (index > -1) {
    return menus[index]
  } else {
    return false
  }
}

export function menu_children(p_menus, parentId) {
  let menus = JSON.parse(JSON.stringify(p_menus))
  let children = menus.filter((item, index) => {
    if (item.isHidden) return false
    return item.parentId == parentId
  })
  children.forEach((item, index) => {
    if (!item.isLeaf) {
      item.children = menu_children(menus, item.id)
    }
  })

  return children
}

export function menu_root_by_path(menus, path) {
  let index = menus.findIndex((item, index) => {
    return item.path == path
  })

  if (index == -1) {
    return false;
  }
  let root = parseInt(menus[index].depth)
  return root
}

export function menu_by_path(menus, path) {
  let index = menus.findIndex((item, index) => {
    return item.path == path
  })
  if (index == -1) {
    return false;
  }
  return menus[index]
}
